<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-link type="primary" href="/download/import_template.xlsx">导入模板下载</el-link>
      <el-upload
          style="margin-left: 16px;display: inline-block;"
          ref="uploadRef"
          :on-change="handleChange"
          :action="APIURL"
          :headers="{'X-Token':token, 'Authorization': token}"
          accept=".xlsx"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :before-upload="beforeUpload">
        <el-button size="small" type="primary" plain icon="el-icon-upload2">批量导入</el-button>
      </el-upload>
      <el-button size="small" type="primary" plain icon="el-icon-download" style="margin-left: 16px;" @click="exportData">导出</el-button>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="add" style="margin-left: 16px;">新增</el-button>
    </div>
    <div style="width: 100%; margin-top: 8px;">
      <el-form :model="searchForm" label-width="80px">
        <div style="display: flex;">
          <el-form-item label="关键字">
            <el-input v-model="searchForm.searchValue" size="small" placeholder="车牌号 | 驾驶员 | 部门" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="审批状态" style="margin-left: 10px;">
            <el-select v-model="searchForm.examineStatus" placeholder="请选择" style="width: 100px;">
              <el-option
                  v-for="item in examineOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="登记时间" style="margin-left: 10px;">
            <el-date-picker
                v-model="searchForm.searchBeginDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择开始时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label-width="10px">
            <el-date-picker
                style="display: inline-block;"
                v-model="searchForm.searchEndDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择结束时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-button-group style="margin-left: 16px;">
            <el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
            <el-button @click="refresh">重置</el-button>
          </el-button-group>
        </div>
      </el-form>
    </div>

    <el-divider />

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" stripe>
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="车牌号" prop="carNo" />
      <!--
      <el-table-column label="车辆负责人" prop="personInChargeName" />
      <el-table-column label="手机" prop="personInChargePhone" />
      -->
      <el-table-column label="车辆类型">
        <template slot-scope="scope">
          <span>{{ getVehicleTypeName(scope.row.vehicleType)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆负责部门" prop="deptName" />
      <el-table-column label="驾驶员姓名" prop="driverName" />
      <el-table-column label="驾驶员电话" prop="driverPhone" />
      <el-table-column label="有效期限至" width="240">
        <template slot-scope="scope">
          <span>
            <span>{{ scope.row.endDate }}</span>
            <template v-if="scope.row.examineStatus === 1">
              <el-button plain type="warning" size="mini" @click="applyDelay(scope.row)" style="margin-left: 6px;">
                续期
              </el-button>
            </template>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="审批状态"
        width="80"
        align="center">
        <template slot-scope="scope">
          <span>
            <template v-if="scope.row.examineStatus === 0">待审</template>
            <template v-else-if="scope.row.examineStatus === 1">已通过</template>
            <template v-else>已拒绝</template>
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button circle type="success" icon="el-icon-more" size="mini" @click="openDetail(scope.row)" title="详情"></el-button>
          <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)" title="修改"></el-button>
          <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.id)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="fetchData"  style="text-align: center;"/>

    <el-dialog :visible.sync="detailDialogVisible" title="详情">
      <el-form :model="detailData" label-width="120px" label-position="right" v-if="detailData">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="ID">
              <span>{{detailData.id}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批状态">
              <span>{{detailData.examineStatus === 0 ? '待审' : (detailData.examineStatus === 1 ? '已通过' : '已拒绝')}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carNo">
              <span>{{detailData.carNo}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆负责部门" prop="deptName">
              <span>{{detailData.deptName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效起始日期" prop="startDate">
              <span>{{detailData.startDate}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效截止日期" prop="endDate">
              <span>{{detailData.endDate}}</span>
            </el-form-item>
          </el-col>
          <!--
          <el-col :span="12">
            <el-form-item label="车辆负责人" prop="personInChargeName">
              <span>{{detailData.personInChargeName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机" prop="personInChargePhone">
              <span>{{detailData.personInChargePhone}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工号">
              <span>{{detailData.employeeNumber}}</span>
            </el-form-item>
          </el-col>
          -->
          <el-col :span="12">
            <el-form-item label="驾驶员姓名" prop="driverName">
              <span>{{detailData.driverName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶员手机" prop="driverPhone">
              <span>{{detailData.driverPhone}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号">
              <span>{{detailData.driverIdcard}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌类型">
              <span>{{getPlateTypeName(detailData.plateType)}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌颜色">
              <span>{{getPlateColorName(detailData.plateColor)}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆类型">
              <span>{{getVehicleTypeName(detailData.vehicleType)}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆颜色">
              <span>{{getVehicleColorName(detailData.vehicleColor)}}</span>
            </el-form-item>
          </el-col>
          <!--
          <el-col :span="12">
            <el-form-item label="海康ID">
              <span>{{detailData.vehicleId}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="同步状态">
              <span>{{detailData.isSync===1 ? '已同步' : '未同步'}}</span>
            </el-form-item>
          </el-col>
          -->
          <el-col :span="24">
            <el-form-item label="备注">
              <span>{{detailData.remarks}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType === 'modify' ? '修改' : '新增'">
      <el-form ref="dataForm" :model="formData" label-width="120px" label-position="right" :rules="rules">
        <el-row :gutter="20">
          <template v-if="formData.id">
            <el-col :span="12">
              <el-form-item label="ID">
                <span>{{formData.id}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="审批状态">
                <span>{{formData.examineStatus === 0 ? '待审' : (formData.examineStatus === 1 ? '已通过' : '已拒绝')}}</span>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carNo">
              <template v-if="formData.id">
                <span>{{formData.carNo}}</span>
              </template>
              <template v-else><el-input v-model.trim="value2Upper" placeholder="请输入车牌号" /></template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆负责部门" prop="deptName">
              <el-input v-model="formData.deptName" placeholder="请输入部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogType !== 'modify' || formData.examineStatus === 0">
            <el-form-item label="有效起始日期" prop="startDate">
              <el-date-picker
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  v-model="formData.startDate"
                  align="right"
                  type="datetime"
                  placeholder="选择日期"
                  :picker-options="pickerOptions"
                  style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogType !== 'modify' || formData.examineStatus === 0">
            <el-form-item label="有效截止日期" prop="endDate">
              <el-date-picker
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  v-model="formData.endDate"
                  align="right"
                  type="datetime"
                  placeholder="选择日期"
                  :picker-options="pickerOptions"
                  style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!--
          <el-col :span="12">
            <el-form-item label="车辆负责人" prop="personInChargeName">
              <el-input v-model="formData.personInChargeName" placeholder="请输入车辆负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机" prop="personInChargePhone">
              <el-input v-model="formData.personInChargePhone" placeholder="请输入负责人手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工号">
              <el-input v-model="formData.employeeNumber" placeholder="请输入员工号" />
            </el-form-item>
          </el-col>
          -->
          <el-col :span="12">
            <el-form-item label="驾驶员姓名" prop="driverName">
              <el-input v-model="formData.driverName" placeholder="请输入驾驶员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶员手机" prop="driverPhone">
              <el-input v-model="formData.driverPhone" placeholder="请输入驾驶员手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="driverIdcard">
              <el-input v-model="formData.driverIdcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌类型" prop="plateType">
              <el-select v-model="formData.plateType" placeholder="请选择">
                <el-option
                    v-for="item in plateTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌颜色" prop="plateColor">
              <el-select v-model="formData.plateColor" placeholder="请选择">
                <el-option
                    v-for="item in plateColorOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆类型" prop="vehicleType">
              <el-select v-model="formData.vehicleType" placeholder="请选择">
                <el-option
                    v-for="item in vehicleTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆颜色" prop="vehicleColor">
              <el-select v-model="formData.vehicleColor" placeholder="请选择">
                <el-option
                    v-for="item in vehicleColorOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" :rows="2" v-model="formData.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="delayDialogVisible" title="续期申请">
      <el-form ref="delayFormRef" :model="delayForm" label-width="120px" label-position="right" :rules="delayRules">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="车牌号">
              <template v-if="delayForm.id">
                <span>{{delayForm.carNo}}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="续期至" prop="endDate">
              <el-date-picker
                  v-model="delayForm.endDate"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  align="right"
                  type="datetime"
                  placeholder="选择日期"
                  :picker-options="pickerOptions"
                  style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="delayDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submitDelay">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import store from '@/store'
  import {
    getList,
    longTimeCar,
    longTimeCarEdit,
    longTimeCarDelete,
    exportData
  } from '@/api/long'
  import {
    deepClone
  } from '@/utils'
  import date from 'date-and-time';
  import {isCellPhone, isPlateNo, isIdCard} from '@/utils/validate'

  export default {
    name: 'interiorVehicle',
    components: {
      Pagination
    },
    computed : {
      value2Upper : {
        get: function(){
          return this.formData.carNo;
        },
        set : function(val){
          this.formData.carNo = val.toUpperCase();
        }
      }
    },
    data() {
      var checkPhone = (rule, value, callback) => {
        if (!value) {
          // callback(new Error("请输入手机号"));
          callback();
        } else if (!isCellPhone(value)) {//引入methods中封装的检查手机格式的方法
          callback(new Error("请输入正确的手机号!"));
        } else {
          callback();
        }
      };
      var checkPlate = (rule, value, callback) => {
        if (!value) {
          callback(new Error("请输入车牌号"));
        } else if(!isPlateNo(value)) {
          return callback(new Error("请输入正确的车牌号"));
        } else {
          callback();
        }
      };
      var checkIdCard = (rule, value, callback) => {
        if (!value) {
          // callback(new Error("请输入身份证号"));
          callback();
        } else if (!isIdCard(value)) {//引入methods中封装的检查手机格式的方法
          callback(new Error("请输入正确的身份证号!"));
        } else {
          callback();
        }
      };
      var checkEmpty = (rule, value, callback) => {
        console.log(rule, value)
        if (!value) {
          callback(new Error("必填项不能为空！"));
        } else {
          callback();
        }
      };
      return {
        examineOptions: [
          {label: '全部', value: ''},
          {label: '待审', value: 0},
          {label: '已通过', value: 1},
          {label: '已拒绝', value: 2},
        ],
        detailData: null,
        detailDialogVisible: false,
        personOptions: [],
        url: '',
        filename: '',
        APIURL: process.env.VUE_APP_BASE_API + '/system/longTimeCar/importData',
        token: store.getters.token,
        total: 0,
        list: [],
        menus: [],
        listLoading: false,
        searchForm: {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          examineStatus: ''
        },
        uploadUrl: '',
        dialogVisible: false,
        dialogType: 'create',
        loading: false,
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '明天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }, {
            text: '30天后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit('pick', date);
            }
          }]
        },
        formData: {
          carNo: '',
          personInChargeName: '',
          personInChargePhone: '',
          employeeNumber: '',
          deptName: '',
          driverName: '',
          driverPhone: '',
          driverIdcard: '',
          endDate: '',
          examineStatus: 0,
          plateType: 0,
          plateColor: 0,
          vehicleType: 1,
          vehicleColor: 0
        },
        rules: {
          carNo: [{ required: true, validator: checkPlate, trigger: 'blur' }],
          startDate: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          endDate: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          // personInChargeName: [{ required: false, messages: '负责人姓名必填', trigger: 'blur' }],
          // personInChargePhone: [{ required: false, validator: checkPhone, trigger: 'blur' }],
          deptName: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          driverName: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          driverPhone: [{ required: false, validator: checkPhone, trigger: 'blur' }],
          driverIdcard: [{ required: false, validator: checkIdCard, trigger: 'blur' }],
          plateType: [{ type: 'number', required: true, trigger: 'blur' }],
          plateColor: [{ type: 'number', required: true, trigger: 'blur' }],
          vehicleType: [{ type: 'number', required: true, trigger: 'blur' }],
          vehicleColor: [{ type: 'number', required: true, trigger: 'blur' }],
        },
        delayForm: {
          id: '',
          carNo: '',
          endDate: '',
          examineStatus: 0
        },
        delayRules: {
          endDate: [{ required: true, validator: checkEmpty, trigger: 'blur' }]
        },
        delayDialogVisible: false,
        // 车牌类型
        plateTypeOptions: [
          {value: 0, label: '标准民用车/军车'},
          {value: 1, label: '02式民用车牌'},
          {value: 2, label: '武警车车牌'},
          {value: 3, label: '警车车牌'},
          {value: 4, label: '民用车双行尾牌'},
          {value: 5, label: '使馆车牌'},
          {value: 6, label: '农用车车牌'},
          {value: 7, label: '摩托车'},
          {value: 8, label: '新能源车'},
        ],
        plateColorOptions: [
          {value: 0, label: '蓝色'},
          {value: 1, label: '黄色'},
          {value: 2, label: '白色'},
          {value: 3, label: '黑色'},
          {value: 4, label: '绿色'},
          {value: 5, label: '民航黑色'},
          {value: 255, label: '其他颜色'}
        ],
        vehicleTypeOptions: [
          {value: 0, label: '其他车'},
          {value: 1, label: '小型车'},
          {value: 2, label: '大型车'},
          {value: 3, label: '摩托车'}
        ],
        vehicleColorOptions: [
          {value: 0, label: '其他颜色'},
          {value: 1, label: '白色'},
          {value: 2, label: '银色'},
          {value: 3, label: '灰色'},
          {value: 4, label: '黑色'},
          {value: 5, label: '红色'},
          {value: 6, label: '深蓝色'},
          {value: 7, label: '蓝色'},
          {value: 8, label: '黄色'},
          {value: 9, label: '绿色'},
          {value: 10, label: '棕色'},
          {value: 11, label: '粉色'},
          {value: 12, label: '紫色'}
        ]
      }
    },
    mounted() {
      console.log('token', this.token)
      this.fetchData()
    },
    methods: {
      getPlateTypeName(val) {
        for (let i = 0; i < this.plateTypeOptions.length; i++) {
          if (this.plateTypeOptions[i].value === val) {
            return this.plateTypeOptions[i].label
          }
        }
        return '未知'
      },
      getPlateColorName(val) {
        for (let i = 0; i < this.plateColorOptions.length; i++) {
          if (this.plateColorOptions[i].value === val) {
            return this.plateColorOptions[i].label
          }
        }
        return '未知'
      },
      getVehicleTypeName(val) {
        for (let i = 0; i < this.vehicleTypeOptions.length; i++) {
          if (this.vehicleTypeOptions[i].value === val) {
            return this.vehicleTypeOptions[i].label
          }
        }
        return '未知'
      },
      getVehicleColorName(val) {
        for (let i = 0; i < this.vehicleColorOptions.length; i++) {
          if (this.vehicleColorOptions[i].value === val) {
            return this.vehicleColorOptions[i].label
          }
        }
        return '未知'
      },
      exportData() {
        let queryParams = {
          carNo: this.searchForm.carNo,
          searchValue: this.searchForm.searchValue,
          searchBeginDate: this.searchForm.searchBeginDate,
          searchEndDate: this.searchForm.searchEndDate,
          examineStatus: this.searchForm.examineStatus
        }
        exportData(queryParams).then(response => {
          if(response.code === 200) {
            let filename = response.msg
            let url = '/download/' + filename
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", filename); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
          }
        })
      },
      applyDelay(row) {
        this.delayForm = {
          id: row.id,
          carNo: row.carNo,
          endDate: row.endDate,
          examineStatus: 0
        }
        this.delayDialogVisible = true
      },
      openDetail(row) {
        this.detailData = row
        this.detailDialogVisible = true
      },
      // 选中的上传改变时检查其文件大小
      handleChange (file) {
        const isLt = file.size / 1024 / 1024 < 1024 // 这里做文件大小限制
        if (!isLt) {
          let fileList = this.$refs.uploadRef.uploadFiles
          console.log(fileList)
          fileList.map((item, index) => {
            if (item.size / 1024 / 1024 > 1024) {
              fileList.splice(index, 1)
            }
          })
          this.$message({
            message: '上传文件大小不能超过 1G!',
            type: 'warning'
          })
        }
        return false
      },
      handleUploadSuccess (res, file) {
        console.log('res', res, file)
        if (res.code === 200) {
          this.url = res.data
          this.filename = file.name
          this.$message.success("上传成功！")
          // 刷新表格数据
          this.refresh()
        } else {
          if (res.msg) {
            this.$message.warning('上传失败：' + res.msg)
          } else {
            this.$message.warning('上传失败！未知原因')
          }
        }
      },
      // 上传前进行文件后缀名检查
      beforeUpload (file) {
        console.log('file.type', file.type)
        const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        const isLt10M = file.size / 1024 / 1024 < 10

        if (!isXLSX) {
          this.$message.error('上传文件只能是 xlsx 格式!')
        }
        if (!isLt10M) {
          this.$message.error('上传文件大小不能超过 10MB!')
        }
        return isXLSX && isLt10M
      },
      search() {
        this.fetchData()
      },
      refresh() {
        this.searchForm = {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          examineStatus: ''
        }
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        getList(this.searchForm).then(response => {
          if (response.code === 200) {
            this.list = response.rows
            this.total = response.total
          } else {
            this.$message.error(response.msg)
          }
          this.listLoading = false
        })
      },
      add() {
        this.formData = {
          carNo: '',
          personInChargeName: '',
          personInChargePhone: '',
          employeeNumber: '',
          deptName: '',
          driverName: '',
          driverPhone: '',
          driverIdcard: '',
          startDate: '',
          endDate: '',
          examineStatus: 0,
          plateType: 0,
          plateColor: 0,
          vehicleType: 1,
          vehicleColor: 0
        }
        let d = new Date()
        this.formData.startDate = date.format(d, 'YYYY-MM-DD')
        // 计算下一年
        d.setFullYear(d.getFullYear()+1);
        d.setDate(d.getDate()-1);
        this.formData.endDate = date.format(d, 'YYYY-MM-DD')
        this.dialogType = 'create'
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      edit(row) {
        this.formData = {
          id: row.id,
          carNo: row.carNo,
          personInChargeName: row.personInChargeName,
          personInChargePhone: row.personInChargePhone,
          employeeNumber: row.employeeNumber,
          deptName: row.deptName,
          driverName: row.driverName,
          driverPhone: row.driverPhone,
          driverIdcard: row.driverIdcard,
          startDate: row.startDate,
          endDate: row.endDate,
          examineStatus: row.examineStatus,
          plateType: row.plateType,
          plateColor: row.plateColor,
          vehicleType: row.vehicleType,
          vehicleColor: row.vehicleColor
        }
        this.dialogType = 'modify'
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      del(id) {
        this.$confirm('确认删除该条数据吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          longTimeCarDelete(id).then(response => {
            if (response.code === 200){
              this.fetchData()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            } else {
              this.$message.error("删除失败：" + response.msg)
            }
          })
        })
      },
      submitDelay() {
        this.$refs['delayFormRef'].validate(valid => {
          if (valid) {
            if (this.checkTime(this.delayForm.startDate, this.delayForm.endDate)) {
              this.$message.warning('截止日期应大于起始日期！')
              return
            }
            longTimeCarEdit(this.delayForm).then(response => {
              if (response.code === 200) {
                this.$message.success('延时申请已提交，请等待审批')
                this.fetchData()
                this.delayDialogVisible = false
              } else {
                this.$message.error("延时申请失败：" + response.msg)
              }
            })
          }
        })
      },
      checkTime(startDate, endDate) {
        let start = new Date(startDate)
        let end = new Date(endDate)
        if(end > start) {
          return true
        }
        return false
      },
      submit() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            var form = deepClone(this.formData)
            if (!this.checkTime(form.startDate, form.endDate)) {
              this.$message.warning('截止日期应大于起始日期！')
              return
            }
            if (!form.id) {
              longTimeCar(form).then(response => {
                if (response.code === 200) {
                  this.$message.success('新增成功')
                  this.fetchData()
                  this.dialogVisible = false
                } else {
                  this.$message.error("新增失败：" + response.msg)
                }
              })
            } else {
              // 置成未审批状态
              form.examineStatus = 0
              longTimeCarEdit(form).then(response => {
                if (response.code === 200) {
                  this.$message.success('修改成功')
                  this.fetchData()
                  this.dialogVisible = false
                } else {
                  this.$message.error("修改失败：" + response.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-upload {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .el-icon-plus.avatar-uploader-icon {
      border: 1px dashed #d9d9d9 !important;
      border-radius: 6px;
      font-size: 28px;
      color: #8c939d;
      width: 128px;
      height: 128px;
      line-height: 128px;
      text-align: center;
    }
  }

  .avatar-uploader {
    height: 128px;

    img {
      width: 128px;
      height: 128px;
    }
  }
</style>
